package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("menu")
public class MenuController {
    @Reference
    private MenuService menuService;
    @RequestMapping("findMenuByUserName")
    public Result findMenuByUserName(@RequestParam("username")String username){
        try {
            List<Map> map=menuService.findMenuByUserName(username);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.GET_MENU_FAIL);
        }
    }
    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult=menuService.findPage(queryPageBean.getPageSize(),queryPageBean.getQueryString(),queryPageBean.getCurrentPage());
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }
    @RequestMapping("findById")
    public Result findById(@RequestParam("id")Integer id){
        try {
            Menu menu=menuService.findById(id);
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }
    @RequestMapping("findAll")
    public Result findAll(){
        try {
            List<Role> roles=menuService.findAll();
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,roles);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }
    @RequestMapping("findroleId")
    public Result findroleId(@RequestParam("id")Integer id){
        try {
            List<Integer> roleId=menuService.findroleId(id);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,roleId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }
    @RequestMapping("deleteById")
    public Result deleteById(@RequestParam("id")Integer id){
        try {
            menuService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MENU_FAIL);
        }
    }
    @RequestMapping("edit")
    public Result edit(@RequestParam("roleIds") Integer [] roleIds,@RequestBody Menu menu){
        try {
            menuService.edit(roleIds,menu);
            return new Result(true,MessageConstant.EDIT_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MENU_FAIL);
        }
    }
    @RequestMapping("addMenu")
    public Result addMenu(@RequestParam("roleIds")Integer [] roleIds,@RequestBody Menu menu){
        try {
            menuService.addMenu(roleIds,menu);
            return new Result(true,MessageConstant.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.ADD_MENU_FAIL);
        }
    }
}
